//header js
var _account_service = require('service/account-service.js');
var _catalog_service = require('service/catalog-service.js');
$('#SearchResult').hide();
var _util = require('util/util.js');
var listTemplate = require('./index.string');

var header = {
    listData: {
        productList: {},
        productList1: {},
    },

    init: function () {

        this.bindEvents();
        this.loadAccountInfo();
        return this;
    },
    //点击事件
    bindEvents: function () {

        $('#logOut').on('click', function () {
            if (confirm("确定要退出吗？")) {
                _account_service.logOut(
                    function (resp) {
                        if (resp === true) {
                            $('#signOnSpan').show();
                            $('#signOutSpan').hide();
                            alert("退出成功")
                            window.location.href = './account-login.html';

                        }
                        else {
                            console.log("退出失败");
                        }
                    },
                    function (errorMsg) {
                        console.log(errorMsg);
                    }
                )

            }
        });
        //购物车
        $('#cart').on('click', function () {
            window.location.href = './cart.html';
        });
        $('#toMain').on('click', function () {
            window.location.href = './catalog-main.html';
        });
        $('#myAccount').on('click', function () {
            window.location.href = './account-myAccount.html';
        });
        $('#myOrder').on('click', function () {
            window.location.href = './order-myOrder.html';
        });


        var _this = this;
        $('#keyword').on('keyup', function () {
            var keyword = { keyword: $.trim($('#keyword').val()) };
            console.log(keyword);
            _catalog_service.searchProduct(
                keyword,
                function (res) {
                    console.log("===========");
                    //console.log(res);
                    $('#SearchResult').show();
                    _this.listData.productList = res;
                    _this.listData.productList1 = null;
                    console.log(_this.listData.productList);
                    var result = _util.renderHtml(listTemplate, { listData: _this.listData });
                    console.log(result);
                    $('#SearchResult').html(result);
                },
                function (errMsg) {
                    $('#SearchResult').hide();
                    console.log("错误！！！！！！");
                }
            );
        });


        $('#searchProducts').on('click', function () {
            var keyword = { keyword: $.trim($('#keyword').val()) };
            console.log(keyword);
            _catalog_service.searchProduct(
                keyword,
                function (res) {
                    console.log("===========");
                    //console.log(res);
                    $('#SearchResult').show();
                    _this.listData.productList = res;
                    console.log(_this.listData.productList);
                    var result = _util.renderHtml(listTemplate, { listData: _this.listData });
                    console.log(result);
                    $('#Catalog').html(result);
                    window.location.href = 'catalog-list.html?id=' + keyword.keyword + '&route=search';
                },
                function (errMsg) {
                    $('#SearchResult').hide();
                    console.log("错误！！！！！！");
                }
            );
        });
        // $('#keyword').on('blur', function () {
        //     // 
        //     $('#SearchResult').hide();

        // });

    },
    loadAccountInfo: function () {
        _account_service.checkLogin(
            //成功调用
            function (res) {

                $('#signOnSpan').hide();
                $('#signOutSpan').show();

            },
            //失败调用
            function (errMsg) {
                console.log('未连接服务器');
                $('#signOnSpan').show();
                $('#signOutSpan').hide();
            }

        );

    },


};

module.exports = header.init();